package br.ufpe.cin.mac.middleware.components.concrete;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import br.ufpe.cin.mac.middleware.components.ConcreteComponent;
import br.ufpe.cin.mac.util.Message;
import br.ufpe.cin.mac.util.Queue;

public class Jms extends ConcreteComponent implements JmsMBean {

	private Map buffer;

	private ArrayList queues;

	public Jms() {
		super(Jms.class.getName());
		this.queues = new ArrayList();
		Queue queue = new Queue("Topico");
		queues.add(queue);
	}

	/************************************************************** *
	 * ******************** INTERNAL METHODS ********************** *
	 * ************************************************************ *
	 */

	protected void msgManager(Message msg) {
		List ops = msg.readOperations();
		Iterator it = ops.iterator();
		while (it.hasNext()) {

			String op = (String) it.next();
			if (op.equals("sendResponse")) {
				sendResponse(msg);
			} else if (op.equals("sendRequest")) {
				sendRequest(msg);
			}
		}
	}

	private void sendResponse(Message msg) {
		buffer.put(msg.hashCode(), msg);
		sendNotification(msg, "terP");
	}

	private void sendRequest(Message msg) {
		buffer.put(msg.hashCode(), msg);
		sendNotification(msg, "invU");
	}
}
